package businesslogic.userBl;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.ArrayList;

import org.jb2011.lnf.beautyeye.resources.beautyeye;

import po.userPO.DepartmentDeanPO;
import po.userPO.StudentPO;
import po.userPO.TeacherPO;
import po.userPO.UniversityDeanPO;
import po.userPO.UserPO;
import vo.userVO.DepartmentDeanVO;
import vo.userVO.StudentVO;
import vo.userVO.TeacherVO;
import vo.userVO.UniversityDeanVO;
import vo.userVO.UserVO;
import businesslogicservice.userblservice.AdministratorBLService;
import dataservice.ResultMessage;
import dataservice.userDataService.UserDatabaseService;


public class Administrator extends User implements AdministratorBLService{

	private static final String ADDRESS_STRING="localhost";
	
	public Administrator(String UserID) {
		super(UserID);
		// TODO Auto-generated constructor stub
	}



	@Override
	public ResultMessage addUser(StudentVO vo)throws Exception {
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		

		try{
			System.setSecurityManager(new java.rmi.RMISecurityManager());
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			
			StudentPO userpo=new StudentPO(vo.getId(),vo.getName(),vo.getId(),vo.getDepartmentId(),0,0,vo.getGrade());
			
			System.out.println(userpo.getId());
			System.out.println(userpo.getName());
			System.out.println(userpo.getPassword());
			System.out.println(userpo.getDepartmentId());
			System.out.println(userpo.getUserType());
			ResultMessage msg=userDataService.insert(userpo);
			
			return msg;
			
		}catch(Exception e){
			e.printStackTrace();
			throw e;
			
		}
	}
	
	@Override
	public ResultMessage addUser(TeacherVO vo)throws Exception {
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());

		try{
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			
			TeacherPO userpo=new TeacherPO(vo.getId(),vo.getName(),vo.getId(),vo.getDepartmentId());
			
			ResultMessage msg=userDataService.insert(userpo);
			
			return msg;
			
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}
	@Override
	public ResultMessage addUser(UniversityDeanVO vo)throws Exception {
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			
			UniversityDeanPO userpo=new UniversityDeanPO(vo.getId(),vo.getName(),vo.getId());
			
			ResultMessage msg=userDataService.insert(userpo);
			
			return msg;
			
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public ResultMessage addUser(DepartmentDeanVO vo)throws Exception {
		
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			
			DepartmentDeanPO userpo=new DepartmentDeanPO(vo.getId(),vo.getName(),vo.getId(),vo.getDepartmentId());
			
			ResultMessage msg=userDataService.insert(userpo);
			
			return msg;
			
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}


	@Override
	public ResultMessage deleteUser(UserVO vo)throws Exception {
		
		UserDatabaseService userDataService;
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try {
			userDataService = (UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			UserPO userpo=userDataService.find(vo.getId());
			System.out.println(vo.getId()+"===========================");
			System.out.println(userpo==null);
			System.out.println(userpo.getUserType());
			userDataService.delete(userpo);
			
			return ResultMessage.DELETE_USER_SUCCESS;
		} catch (Exception e) {
			
			e.printStackTrace();
			return ResultMessage.DELETE_USER_FAILURE;
		}
	}

	
	

	

	
	@Override
	
	public ArrayList<UserVO> getUserList(int userType,String departmentID)throws Exception{
		ArrayList<UserVO> userList=new ArrayList<>();
		ArrayList<UserPO> userPOList=new ArrayList<>();
		UserDatabaseService userDataService;
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try {
			userDataService = (UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
		
			if(userType==3||departmentID =="全部")
				userPOList=userDataService.findAll(userType);
			
			else{
				userPOList=userDataService.findAllByDepartment(userType, departmentID);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
		for(UserPO user:userPOList){
			switch (userType) {
			case 0:
				StudentPO studentPO=(StudentPO)user;
				userList.add(new StudentVO(studentPO.getId(), studentPO.getName(),
						studentPO.getDepartmentId(), studentPO.getGrade()));
				break;

			case 1:
				TeacherPO teacherPO=(TeacherPO)user;
				userList.add(new TeacherVO(teacherPO.getId(), teacherPO.getName(), teacherPO.getDepartmentId()));
				break;
				
			case 2:
				DepartmentDeanPO deanPO=(DepartmentDeanPO)user;
				userList.add(new DepartmentDeanVO(deanPO.getId(), deanPO.getName(), deanPO.getDepartmentId()));
				break;
				
			case 3:
				
				UniversityDeanPO universityDeanPO=(UniversityDeanPO)user;
				userList.add(new UniversityDeanVO(universityDeanPO.getId(), universityDeanPO.getName()));
				break;
			}
		}
		
		return userList;
	}
	
	


	public ResultMessage modifyPassword(String userID,String password)throws Exception{
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			ResultMessage msg=userDataService.update(userID, password);
			return msg;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	
	}



	@Override
	public ResultMessage modifyUser(StudentVO vo)throws Exception {
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
		
			StudentPO studentPO=(StudentPO) userDataService.find(vo.getId());
			
			ResultMessage message=userDataService.update(new StudentPO(vo.getId(), vo.getName(), studentPO.getPassword(), vo.getDepartmentId(), studentPO.getTotalCredit(), studentPO.getTotalSelectedCredit(), vo.getGrade()));
		
			return message;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		
	}



	@Override
	public ResultMessage modifyUser(TeacherVO vo)throws Exception {
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
	
			TeacherPO teacherPO=(TeacherPO) userDataService.find(vo.getId());
			ResultMessage message=userDataService.update(new TeacherPO(vo.getId(), vo.getName(), teacherPO.getPassword(), vo.getDepartmentId()));
		
		
			return message;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		
	}



	@Override
	public ResultMessage modifyUser(UniversityDeanVO vo)throws Exception {
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
	
			UniversityDeanPO universityDeanPO=(UniversityDeanPO) userDataService.find(vo.getId());
			ResultMessage message=userDataService.update(new UniversityDeanPO(vo.getId(), vo.getName(), universityDeanPO.getPassword()));
	
			return message;
		
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}



	@Override
	public ResultMessage modifyUser(DepartmentDeanVO vo) throws Exception{
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
		
		
			DepartmentDeanPO departmentDeanPO=(DepartmentDeanPO) userDataService.find(vo.getId());
			ResultMessage message=userDataService.update(new DepartmentDeanPO(vo.getId(), vo.getName(), departmentDeanPO.getPassword(), vo.getDepartmentId()));
		
			return message;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}

	

}
